home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cgazv5n5.arc
/
XMSGEN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-23
|
6KB
|
234 lines
/*--- XMSGEN.C --------------------------- Listing 1 ------
* Core XMS Access Functions called by the other programs
* by David Babcock
*
* Validated for Microsoft and Borland C/C++
*
* This file is called by the other files in this article.
*
* (c) 1991 C Gazette. Object Code may be used freely,
* source code may be used if authorship and publication
* are acknowledged.
*-------------------------------------------------------*/
#include <stdio.h>
#include <dos.h>
#include <limits.h>
#include "xms.h"
int EMBhandle = -1 ;
struct xms_struc_type xms_struc ;
struct ExtMemMoveStruct XMSMoveStruct ;
/* prototypes */
void CDECL closeHMA(void);
void CDECL closeXMS(void);
static int getXMSversion(void);
int openHMA(void);
int openXMS(unsigned int);
void far * swapXMSpage(unsigned int);
unsigned long CDECL XMSfunction(unsigned int,
unsigned int,unsigned int);
unsigned long CDECL XMSfunction ( unsigned int func_no,
unsigned int dx_or_ds, unsigned int bx_or_si )
{
/* We _cdecl this if using MSC because MSC 6.0
generated incorrect code with the fastcall
option. The bug was fixed in 6.00a but we
can't assume everyone upgraded. */
unsigned int tu ;
void far *p ;
p = ( void far * ) xms_struc.func ;
asm push ds
asm push si
asm mov ax,func_no
asm mov ah,al
asm mov bx,bx_or_si
asm mov si,bx
asm mov dx,dx_or_ds
asm mov ds,dx
asm call dword ptr [p]
asm pop si
asm pop ds
asm push ax
asm mov tu,bx
xms_struc.ret_bx = tu ;
asm mov tu,dx
xms_struc.ret_dx = tu ;
asm pop ax
}
void CDECL closeXMS ( void )
{
if ( EMBhandle != -1 )
XMSfunction ( 0x0A, EMBhandle, 0 ) ;
}
static int initXMS ( void )
{
/* Returns XMS version, with major number in high 8 bits
and minor in low 8 bits; returns 0 if no XMS */
int ret = 0 ;
union REGS regs ;
struct SREGS sregs ;
regs.x.ax = 0x4300 ;
int86 ( 0x2F, ®s, ®s ) ;
if ( regs.h.al == 0x80 )
{
/* XMS present */
regs.x.ax = 0x4310 ;
int86x ( 0x2F, ®s, ®s, &sregs ) ;
xms_struc.func = MK_FP ( sregs.es, regs.x.bx ) ;
ret = 1 ;
}
return ( ret ) ;
}
unsigned int basicXMSfunction ( unsigned int func_no,
unsigned int dx_or_ds, unsigned int bx_or_si )
{
int ret ;
ret = XMSfunction ( func_no, dx_or_ds, bx_or_si ) ;
return ( ret ? 0 : xms_struc.ret_bx & 0x00FF ) ;
}
unsigned long dxbxXMSfunction ( unsigned int func_no,
unsigned int handle )
{
int ret ;
ret = XMSfunction ( func_no, handle, 0 ) ;
return ( ret ?
((( unsigned long ) xms_struc.ret_dx ) << 16 ) |
xms_struc.ret_bx :
( unsigned long ) ( xms_struc.ret_bx & 0x00FF )) ;
}
int GetXMSVersionNumber ( void )
{
int ret = 0 ;
if ( initXMS ( ) )
{
ret = XMSfunction ( 0, 0, 0 ) ;
if ( ret > 0x200 )
ret = 0x200 ; /* for Qualitas XMS driver bug */
}
return ( ret ) ;
}
int RequestHighMemoryArea ( unsigned int bytes )
{
return basicXMSfunction ( 1, bytes, 0 ) ;
}
int ReleaseHighMemoryArea ( void )
{
return basicXMSfunction ( 2, 0, 0 ) ;
}
int GlobalEnableA20 ( void )
{
return basicXMSfunction ( 3, 0, 0 ) ;
}
int GlobalDisableA20 ( void )
{
return basicXMSfunction ( 4, 0, 0 ) ;
}
int LocalEnableA20 ( void )
{
return basicXMSfunction ( 5, 0, 0 ) ;
}
int LocalDisableA20 ( void )
{
return basicXMSfunction ( 6, 0, 0 ) ;
}
int QueryA20 ( void )
{
int ret ;
ret = XMSfunction ( 7, 0, 0 ) ;
return ( ( xms_struc.ret_bx & 0x00FF ) == 0 ? ret :
xms_struc.ret_bx & 0x00FF ) ;
}
unsigned long QueryFreeExtendedMemory ( void )
{
int ret ;
ret = XMSfunction ( 8, 0, 0 ) ;
return ((((unsigned long) xms_struc.ret_dx ) << 16 ) | ret);
}
int AllocateExtendedMemoryBlock ( unsigned int kBytes )
{
return basicXMSfunction ( 9, kBytes, 0 ) ;
}
int FreeExtendedMemoryBlock ( unsigned int handle )
{
return basicXMSfunction ( 0xA, handle, 0 ) ;
}
int MoveExtendedMemoryBlock ( void far *p )
{
int ret ;
ret = XMSfunction ( 0xB, FP_SEG(p), FP_OFF(p) ) ;
return ( ret ? 0 : xms_struc.ret_bx & 0x00FF ) ;
}
unsigned long LockExtendedMemoryBlock ( unsigned int handle )
{
return dxbxXMSfunction ( 0xC, handle ) ;
}
int UnlockExtendedMemoryBlock ( unsigned int handle )
{
return basicXMSfunction ( 0xD, handle, 0 ) ;
}
unsigned long GetEMBHandleInformation ( unsigned int handle )
{
return dxbxXMSfunction ( 0xE, handle ) ;
}
int ReallocateExtendedMemoryBlock ( unsigned int handle,
unsigned int size )
{
int ret ;
ret = XMSfunction ( 0xF, handle, size ) ;
return ( ret ? 0 : xms_struc.ret_bx & 0x00FF ) ;
}
int RequestUpperMemoryBlock ( unsigned int paragraphs )
{
int ret ;
ret = XMSfunction ( 0x10, paragraphs, 0 ) ;
return ( ret ? 0 : xms_struc.ret_bx & 0x00FF ) ;
}
int ReleaseUpperMemoryBlock ( unsigned int segment )
{
int ret ;
ret = XMSfunction ( 0x11, segment, 0 ) ;
return ( ret ? 0 : xms_struc.ret_bx & 0x00FF ) ;
}
char *PrintXMSVersionNumber ( unsigned int ver )
{
static char buf [ 6 ] ;
sprintf ( buf, "%d%d.%d%d", ver / 4096,
( ver % 4096 ) / 256, ( ver % 256 ) / 16, ver % 16 ) ;
return ( buf ) ;
}